﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Portal.Nhibernate.Repository;

namespace Portal.Web.Admin.User
{
    public partial class ChangePasswordSave : AdminBasePage
    {
        private readonly IUserRepository<IUser> _userRepository;

        public ChangePasswordSave()
        {
            _userRepository = new UserRepository();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            var newPassword = Fq("newPassword");
            var oldPassword = Fq("oldPassword");
            if (string.IsNullOrEmpty(Fq(newPassword.Trim())))
                Json(new AjaxResult { state = false, message = "原密码不能为空" });
            if (string.IsNullOrEmpty(oldPassword.Trim()))
                Json(new AjaxResult { state = false, message = "新密码不能为空" });
            //查询用户实例
            var model = _userRepository.Get().FirstOrDefault(o => o.Account == CurrentUserName && o.Password == oldPassword);
            if (model == null)
            {
                if (Request.IsAjaxRequest())
                    Json(new AjaxResult { message = "原密码不正确", state = false });
            }
            else
            {
                model.Password = newPassword;
                _userRepository.Update(model);
                _userRepository.Save();
                if (Request.IsAjaxRequest())
                    Json(new AjaxResult { message = "修改成功", state = true });
            }


        }
    }
}